package com.feeyo.groundservice.base_util

import android.annotation.SuppressLint
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.TimeZone

/**
 *作者：QLH on 2023-08-03
 *描述：
 */

/**
 * 2023-07-25这种格式
 * @param format 格式化模板
 * **/
@SuppressLint("SimpleDateFormat")
fun String.toDate(format: String = DateFormatType.FORMAT_YYYY_MM_DD): Date? {
    if (this.length != format.length) {
        throw IllegalArgumentException("字符串日期格式与format不匹配：$this---$format")
    }
    return SimpleDateFormat(format).let {
        it.timeZone = TimeZone.getTimeZone("Asia/Shanghai")
        it.parse(this)
    }
}

/**格式化时间 milliseconds or s
 * @param format 格式化模板
 * **/
fun Long.formatTime(format: String = DateFormatType.FORMAT_DEFAULT): String {
    if (this == 0L) return "".dealString()
    return formatT(format, this)
}

/**格式化时间：2020-09-09 12:35:45
 *
 * s/ms
 * **/
fun Long.formatTimeYMDHMS(): String {
    if (this == 0L) return "".dealString()
    return formatT(DateFormatType.FORMAT_DEFAULT, this)
}

/**@param milliOrSecond  milliseconds o s
 * **/
private fun formatT(format: String, milliOrSecond: Long): String {
    val sdf = SimpleDateFormat(format)
    sdf.timeZone = TimeZone.getTimeZone("Asia/Shanghai")
    return sdf.format(Date(milliOrSecond.getMillTime()))
}


/**格式化时间：2020-09-09
 * milliseconds or s
 * **/
fun Long.formatTimeYMD(): String {
    if (this == 0L) return "".dealString()
    return formatT(DateFormatType.FORMAT_YYYY_MM_DD, this)
}

/**格式化时间：2020-09-09 13
 * milliseconds or s
 * **/

fun Long.formatTimeYMDH(): String {
    if (this == 0L) return "".dealString()
    return formatT(DateFormatType.FORMAT_YYYY_MM_DD_HH, this)
}

/**格式化时间：2020-09-09 13:12
 * milliseconds or s
 * **/
fun Long.formatTimeYMDHM(): String {
    if (this == 0L) return "".dealString()
    return formatT(DateFormatType.FORMAT_YYYY_MM_DD_HH_MM, this)
}

/**格式化时间：2020-09
 * milliseconds or s
 * **/
fun Long.formatTimeYM(): String {
    if (this == 0L) return "".dealString()
    return formatT(DateFormatType.FORMAT_YYYY_MM, this)
}

/**格式化时间：2020
 * milliseconds or s
 * **/
fun Long.formatTimeY(): String {
    if (this == 0L) return "".dealString()
    return formatT(DateFormatType.FORMAT_YYYY, this)
}

/**时间格式化：08:00
 *  milliseconds or s
 * **/
fun Long.formatTimeHM(): String {
    if (this == 0L) return "".dealString()
    return formatT(DateFormatType.FORMAT_HH_MM, this)
}

fun Long.formatTimeMD(): String {
    if (this == 0L) return "".dealString()
    return formatT(DateFormatType.FORMAT_MM_DD, this)
}

/**时间格式化：08:00:00
 * milliseconds or s
 * **/
fun Long.formatTimeHMS(): String {
    if (this == 0L) return "".dealString()
    return formatT(DateFormatType.FORMAT_HH_MM_SS, this)
}

/**格式化时间：08:00(11)
 *
 * @param referenceTime 参考时间
 * milliseconds or s
 * **/
fun Long.formatTimeHMWithDay(
    referenceTime: Long? = System.currentTimeMillis(),
): String {
    if (this == 0L) return "".dealString()
    val t = formatTimeHM()
    val d = formatT(DateFormatType.FORMAT_DD, this)
    return if (formatTimeYMD() == (referenceTime ?: System.currentTimeMillis()).formatTimeYMD())
        t
    else
        "$t($d)"
}

//如果是s则转换成ms
fun Long.getMillTime() = this.let {
    if (it.toString().length == 10) it * 1000 else it
}

//如果是ms则转换成s
fun Long.geSecondTime() = this.let {
    if (it.toString().length == 13) it / 1000 else it
}

/**转化为毫秒:s->ms**/
fun Long.toMillSeconds(): Long = this * 1000

/**转化为秒:ms->s**/
fun Long.toSeconds(): Long = this / 1000

/**获取2021-04-20的day:20**/
fun String?.getDayFromYMD(): String = this?.let {
    val t = this.split("-")
    return if (t.isNotEmpty()) {
        t.last()
    } else "--"
} ?: "--"

/**获取前一天
 *
 * FORMAT_YYYY_MM_DD
 * **/
fun String.getPreDay(): String =
    toDate()?.time?.let {
        val date = it - 24 * 60 * 60 * 1000
        date.formatTimeYMD()
    } ?: "--"

/**获取前一天
 *
 * FORMAT_YYYY_MM_DD
 * **/
fun Long.getPreDay(): Long = this.getMillTime() - 24 * 60 * 60 * 1000

/**获取后一天
 *
 * FORMAT_YYYY_MM_DD
 * **/
fun String.getNextDay(): String =
    toDate()?.time?.let {
        val date = it + 24 * 60 * 60 * 1000
        date.formatTimeYMD()
    } ?: "--"

/**获取后一天
 *
 * FORMAT_YYYY_MM_DD
 * **/
fun Long.getNextDay(): Long = this.getMillTime() + 24 * 60 * 60 * 1000

/**获取月份 2021-04-20的month 04 **/
fun String?.getMonthFromYMD(): String = this?.let {
    val t = this.substringBeforeLast("-").split("-")
    return if (t.isNotEmpty()) {
        t.last()
    } else "--"
} ?: "--"

/**获取上个月
 *
 * @return mills
 * **/
fun Long.getPreMonth(): Long {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = this.getMillTime()
    calendar.add(Calendar.MONTH, -1)
    return calendar.timeInMillis
}

/**获取下个月
 *
 * @return mills
 * **/
fun Long.getNextMonth(): Long {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = this.getMillTime()
    calendar.add(Calendar.MONTH, 1)
    return calendar.timeInMillis
}


/**获取某个日期开始和结束
 *
 *获取2025-12-01 00:00:00和2025-12-01 23:59:59 时间戳
 * **/
fun String.getStartEndTime(): Array<Long> {
    val start = "$this 00:00:00".toDate(DateFormatType.FORMAT_DEFAULT)?.time?.toSeconds() ?: 0L
    val end = "$this 23:59:59".toDate(DateFormatType.FORMAT_DEFAULT)?.time?.toSeconds() ?: 0L
    return arrayOf(start, end)
}


/**获取零点时间***/
fun Long.getZeroTime(): Long {
    val calendar = Calendar.getInstance()
    calendar.timeInMillis = this.getMillTime()
    calendar.set(Calendar.HOUR_OF_DAY, 0)
    calendar.set(Calendar.MINUTE, 0)
    calendar.set(Calendar.SECOND, 0)
    calendar.set(Calendar.MILLISECOND, 0)
    return calendar.timeInMillis / 1000
}